﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MxWeiXinPF.Common;
using System.Text;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class grade_list : MxWeiXinPF.Web.UI.ManagePage
    {
        protected MxWeiXinPF.Model.manager admin_info = null;
        protected LinkButton btnDelete;
        protected HtmlForm form1;
        public string keywords = string.Empty;
        protected LinkButton lbtnSearch;
        protected Repeater rptList;
        protected TextBox txtKeywords;
        protected int wid = 0;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

                base.ChkAdminLevel("user_grade", MxWeiXinPF.Common.MXEnums.ActionEnum.Delete.ToString());
                int num = 0;
                int num2 = 0;
                MxWeiXinPF.BLL.user_grade _grade = new MxWeiXinPF.BLL.user_grade();
                for (int i = 0; i < this.rptList.Items.Count; i++)
                {
                    int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
                    CheckBox box = (CheckBox)this.rptList.Items[i].FindControl("chkId");
                    if (box.Checked)
                    {
                        if (_grade.Delete(id))
                        {
                            num++;
                        }
                        else
                        {
                            num2++;
                        }
                    }
                }
                base.AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), string.Concat(new object[] { "删除用户级别成功", num, "条，失败", num2, "条" }));
                base.JscriptMsg(string.Concat(new object[] { "删除成功", num, "条，失败", num2, "条！" }), Utils.CombUrlTxt("grade_list.aspx", "keywords={0}", new string[] { this.txtKeywords.Text.Trim() }), "Success");

            }
            catch (Exception ex)
            {

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("grade_list.aspx", "keywords={0}", new string[] { this.txtKeywords.Text.Trim() }));
        }

        protected string CombSqlTxt(int _wid, string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (_wid != 0)
            {
                builder.Append(" and wid=" + _wid);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and grade like '%" + _keywords + "%'");
            }
            if (this.admin_info == null)
            {
                this.admin_info = base.GetAdminInfo();
            }
            builder.Append(string.Concat(new object[] { " and (shopid=", this.admin_info.id, " or isnull(company,'')='", this.admin_info.company, "')" }));
            return builder.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.keywords = MXRequest.GetQueryString("keywords");
                MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                this.wid = weiXinCode.id;
                this.admin_info = base.GetAdminInfo();
                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("user_grade", MXEnums.ActionEnum.View.ToString());
                    this.RptBind("id>0" + this.CombSqlTxt(this.wid, this.keywords));
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        private void RptBind(string _strWhere)
        {
            this.txtKeywords.Text = this.keywords;
            this.rptList.DataSource = new MxWeiXinPF.BLL.user_grade().GetList(0, _strWhere, "id asc");
            this.rptList.DataBind();
        }
    }
}